//
//  SegmentedView.m
//  BTRIP
//
//  Created by Rain on 15/11/12.
//  Copyright © 2015年 johnbk. All rights reserved.
//

#import "SegmentedView.h"

// 按钮的宽高
#define kButtonHeight 30
#define kButtonWidth  80

// 按钮间距
#define KSPACE 20

// 按钮选中时颜色
#define KBUTTONTITLECOLOR [UIColor blueColor]

// 按钮未选中时颜色
#define KBUTTONTITLEUNSELECTEDCOLOR [UIColor blackColor]

@interface SegmentedView ()

// 按钮标识条
@property (nonatomic, retain) UIView *slideView;

@end

@implementation SegmentedView

- (void)dealloc
{
    [_btnTitleArr release];
    [_titleTintColor release];
    [_slideTintColor release];
    [_slideView release];
    
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame btnTitleArr:(NSArray *)btnTitleArr {
    
    self = [super initWithFrame:frame];
    if (self) {
        self.btnTitleArr = [NSArray arrayWithArray:btnTitleArr];
    }
    return self;
}

// 当数组传进来值的时候，创建视图
- (void)setBtnTitleArr:(NSArray *)btnTitleArr {
    
    if (_btnTitleArr != btnTitleArr) {
        [_btnTitleArr release];
        _btnTitleArr = [btnTitleArr retain];
    }
    
    UIScrollView *btnScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 5 * SWIDTH, self.frame.size.width, self.frame.size.height - 10 * SWIDTH)];
    
    // 设置contentSize
    btnScrollView.contentSize = CGSizeMake((kButtonWidth + KSPACE) * _btnTitleArr.count, 0);
    
    for (int i = 0; i < _btnTitleArr.count; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(10 * SWIDTH + (kButtonWidth + KSPACE) * i, 5 * SWIDTH, kButtonWidth, kButtonHeight);
        [btn setTitle:_btnTitleArr[i] forState:UIControlStateNormal];
        if (i == 0) {
            [btn setTitleColor:KBUTTONTITLECOLOR forState:UIControlStateNormal];
        } else {
            [btn setTitleColor:KBUTTONTITLEUNSELECTEDCOLOR forState:UIControlStateNormal];
        }
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = i + 1000;
        btnScrollView.tag = 2000;
        [btnScrollView addSubview:btn];
        
    }
    btnScrollView.showsHorizontalScrollIndicator = NO;
    
    self.slideView = [[UIView alloc] initWithFrame:CGRectMake(10 * SWIDTH, 10 * SWIDTH, kButtonWidth, kButtonHeight)];
    self.slideView.backgroundColor = [UIColor grayColor];
    [btnScrollView addSubview:self.slideView];
    [self.slideView release];
    
}

// 按钮点击事件
- (void)btnAction:(UIButton *)sender {
    
    for (UIButton *btnTemp in [self viewWithTag:2000].subviews) {
        if ([btnTemp isKindOfClass:[UIButton class]]) {
            if (btnTemp.tag == sender.tag) {
                [btnTemp setTitleColor:KBUTTONTITLECOLOR forState:UIControlStateNormal];
            } else {
                [btnTemp setTitleColor:KBUTTONTITLEUNSELECTEDCOLOR forState:UIControlStateNormal];
            }
        } else {
            NSLog(@"%@", self.subviews);
        }
    }
    [UIView animateWithDuration:0.1 animations:^{
        self.slideView.frame = CGRectMake(sender.frame.origin.x, self.slideView.frame.origin.y, kButtonWidth, kButtonHeight);
    }];
    // 执行代理方法
    [self.delegate buttonOfSegmentedViewClicked:sender];
    
}

- (void)setButtonTag:(NSInteger)buttonTag {
    
    UIButton *button = (UIButton *)[self viewWithTag:buttonTag];
    [self btnAction:button];
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
